package org.oneblog.service;

import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.oneblog.data.util.StringTools;

public class AbstractArticleService {

    static final Pattern url = Pattern.compile(".*!.*\\((http[s]{0,1}:.*\\.jpg).*");
    
    protected static String getImgUrl(String content) {
        
        if(StringUtils.isBlank(content)) {
            return "";
        }
        
        String[] lines = content.split("\n+");
        
        for(String line : lines) {
            if(line.indexOf("http") >= 0) {
                String img = StringTools.getRegexString(line, url);
                if(StringUtils.isNotBlank(img)) {
                    return img;
                }
            }
        }
        
        return null;
    }

    private static final String PREFIX_1 = "https://oomabc.com";
    private static final String PREFIX_NEW = "http://localhost:8081";
    //https://oomabc.com/staticsrc/img/202006/02/1591084957568_f3a5d68b3c5b4287b17b67cd7aafe172.jpg
    //http://localhost:8081/
    protected static String convertHostToLocal(String s) {
        if (s == null) {
            return s;
        }
        if (s.startsWith(PREFIX_1)) {
            return PREFIX_NEW + s.substring(PREFIX_1.length());
        }

        return s;
    }
    
    public static void main(String[] args) {
        System.out.println(getImgUrl("![图片](http://gcd.zhuiapp.com//static/img/b03.jpg \"图片信息\")"));

        System.out.println(convertHostToLocal("https://oomabc.com/staticsrc/img/202006/02/1591084957568_f3a5d68b3c5b4287b17b67cd7aafe172.jpg"));
    }


}
